<?php





namespace Miniprogram\Controller;





use Think\Controller;



class TariffController extends Controller

{

    public function index(){

        $product_id=I('product_id');

        $cartype=I('cartype_id');

        if(!$typeInfo=M('cartype')->field('id,title,flag_fall_price,over_charge')->find($cartype))

            error($this->config['parr_err']);

        if(!$productInfo=M('product')->field('id,title,floor_cost')->find($product_id))

            error($this->config['parr_err']);

        $productInfo['floor_cost']=json_decode($productInfo['floor_cost']);

        $attachment=M('attachment')->where(['product_id'=>$product_id])->select();

        $attachmentIds=array_column($attachment,'id');

        $options=M('attachment_option')->where(['attachment_id'=>['in',$attachmentIds]])->select();

        $option=[];

        foreach ($options as $item){

            $item['num']='0';

            $option[$item['attachment_id']][]=$item;

        }

        $large=$dismounting=$packages=$flat=$additional=[];

        foreach ($attachment as $key=> $item){

            $data['event']=$item;

            $data['option']=$option[$item['id']];

            switch ($item['type']){

                case '1':

                    $large[]=$data;

                    break;

                case '2':

                    $dismounting[]=$data;

                    break;

                case '3':

                    $packages[]=$data;

                    break;

                case '4':

                    $flat[]=$data;

                    break;

                case '5':

                    $additional[]=$data;

                    break;

            }

        }

        $data=[

            'productInfo'=>$productInfo,

            'cartypeInfo'=>$typeInfo,

            'attachment'=>[

                'large'=>$large,

                'dismounting'=>$dismounting,

                'packages'=>$packages,

                'flat'=>$flat,

                'additional'=>$additional

            ],
            'tel'=>M('config')->where(['key'=>'Tel'])->getField('value')

        ];

        success($data);

    }

}